<?php

class NewslettersController extends AppController {

    var $name = "Newsletters";
    var $uses = array('User', 'Newsletter');
    var $components = array('Email');

    function beforeFilter() {
        parent::beforeFilter();
        // echo $this->Session->setFlash("Welcome user");
        $this->Auth->userScope = array('User.role =' => 'user');
        $this->Auth->userScope = array('User.status' => 1);

        $this->Auth->allow('subscribe');
        $this->Auth->loginRedirect = array('controller' => 'Homes', 'action' => 'index');
    }

    function admin_index() {
        $this->layout = "admin_default";
        $this->set('email', $this->Newsletter->find('list', array('fields' => array('email')), array('conditions' => array('status' => '0'))));
    }

    function admin_edit() {
        $this->layout = "admin_default";
    }

    function admin_delete() {
        
    }

    function admin_send() {
        $this->layout = "admin_default";
        if (!empty($this->data)) {
            $emails = $this->data['Newsletter'];
            $a = implode(' ', $emails['emails']);
            $email = str_replace(' ', ',', $a);
            $this->Email->to = $email;  // sending email to subscriber
            $this->Email->subject = 'NewsLetter for focusApps';
            $this->Email->replyTo = 'noreply@example.com';
            $this->Email->from = 'News Letter from Rate The Universe <noreply@example.com>';
           // $this->Email->template = 'emailtemplate'; // note no '.ctp'
            $this->Email->sendAs = 'both';
            //seperate line in the message body.
            if ($this->Email->send($this->data['Newsletter']['msg'])) {
                $this->Session->setFlash('Email sent');
            } else {
                $this->Session->setFlash('Email not sent');
            }
            $this->redirect(array('controller' => 'Newsletters', 'action' => 'newslettersent'));
        }
    }

    function admin_newslettersent() {
        $this->layout = "admin_default";
    }

    function subscribe() {
        if ($this->RequestHandler->isAjax()) {
            $this->layout = "";
            $this->autoRender = false;
            $response=false; 
            
            $data = $this->Newsletter->find('first', array('conditions' => array('Newsletter.email' => $_POST['email'])));
            if ($data) {
                $response='Already Subscribed';
            } else {
               $this->data['Newsletter']['email']=$_POST['email'];
                $this->data['Newsletter']['ip']=$this->RequestHandler->getClientIp();
                $this->Newsletter->set($this->data);
                if ($this->Newsletter->save($this->data)) {
                $response='Thanks for Subscribing. You will soon hear from us.';    
                } else {
                    if (isset($this->Newsletter->validationErrors['email'])) {
                        $response='Not Subscribed. Try again later';
                    }
                }
            }
            echo $response;
        }
    }

}

?>
